#include "Property.h"

namespace PropertyDeserializer
{
	IProperty* fromXmlString(const char* xml)
	{
		IProperty* p = 0;
		TiXmlDocument* doc = new TiXmlDocument();
		doc->Parse(xml);
		if(doc->Error())
			{
//              throw std::exception("The TiXmlDocument reported an error");
				delete doc;
				return p;
			}

		TiXmlElement* root = doc->RootElement();
		if(strcmp(root->Value(), "Property") != 0)
			{
				delete doc;
				return p;
			}

		const char* name = root->Attribute("type");
		const char* type = root->Attribute("type");
		const char* value = root->Attribute("value");

		switch(TypeInfo::getType(type))
			{
			case BOOL:
				p = dynamic_cast<IProperty*>(new Property<bool>(name, Ogre::StringConverter::parseBool(value)));
				break;
			case INT:
				p = dynamic_cast<IProperty*>(new Property<int>(name, Ogre::StringConverter::parseInt(value)));
				break;
			case FLOAT:
				p = dynamic_cast<IProperty*>(new Property<float>(name, Ogre::StringConverter::parseReal(value)));
				break;
			case STRING:
				p = dynamic_cast<IProperty*>(new Property<std::string>(name, value));
				break;
				
			case VECTOR2:
				p = dynamic_cast<IProperty*>(new Property<Ogre::Vector2>(name, Ogre::StringConverter::parseVector2(value)));
				break;
			case VECTOR3:
				p = dynamic_cast<IProperty*>(new Property<Ogre::Vector3>(name, Ogre::StringConverter::parseVector3(value)));
				break;
			case VECTOR4:
				p = dynamic_cast<IProperty*>(new Property<Ogre::Vector4>(name, Ogre::StringConverter::parseVector4(value)));
				break;
			case QUATERNION:
				p = dynamic_cast<IProperty*>(new Property<Ogre::Quaternion>(name, Ogre::StringConverter::parseQuaternion(value)));
				break;
				
			default:
				break;
			}
		
		delete doc;
		
		return p;
	}
}
